package edu.sandau.b230111.b23011125.Controller;


import edu.sandau.b230111.b23011125.Global.GlobalDefine;
import edu.sandau.b230111.b23011125.Global.RecordWithNameTitile;
import edu.sandau.b230111.b23011125.Model.SubmitRecord;

import javax.servlet.http.HttpServletRequest;

import edu.sandau.b230111.b23011125.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.*;

@Controller
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProblemService problemService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private ContestService contestService;

    @RequestMapping("/record/list")
    public String recordList(HttpServletRequest request, ModelMap map,@RequestParam(value = "problemid",defaultValue = "ALL") String problemid,@RequestParam(value = "page",defaultValue = "0") String pageCnt){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        Page<SubmitRecord> submitRecords ;
        if(problemid.equals("ALL")){
            submitRecords = recordService.queryAllRecord(Integer.parseInt(pageCnt),GlobalDefine.PAGESIZE);

        }
        else{
            submitRecords = recordService.queryRecordByProblemId(problemid,Integer.parseInt(pageCnt),GlobalDefine.PAGESIZE);
        }
        List<RecordWithNameTitile> records = new ArrayList<>();
        List<SubmitRecord> submitRecordContent = submitRecords.getContent();
        for(SubmitRecord x :submitRecordContent){
            RecordWithNameTitile y = new RecordWithNameTitile();
            y.setProblemid(x.getProblemid());
            y.setUserid(x.getUserid());
            y.setRecordid(x.getRecordid());
            y.setSendtime(x.getSendtime());
            y.setStatus(x.getStatus());

            y.setProblemtitle(problemService.queryProblemById(x.getProblemid()).getTitle());
            y.setUsername(userService.queryUserInfoByUserId(x.getUserid()).getName());
            records.add(y);
        }
        map.put("records",records);
        map.put("totalElement",submitRecords.getTotalElements());
        map.put("totalPage",submitRecords.getTotalPages());
        map.put("nowPage",submitRecords.getNumber());
        map.put("problemid",problemid);
        return "record_list";
    }

    @RequestMapping("/api/record/add")
    public String recordAdd(HttpServletRequest request,@RequestParam(value = "problemid",defaultValue = "ALL") String problemid,@RequestParam(value = "contestid",defaultValue = "NONE") String contestid){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        Random random = new Random();
        String result = "等待中";
        int randomresult = (random.nextInt(4))+1;
        if(randomresult==1) {
            result = "编译错误";
        }
        else if(randomresult==2){
            result = "答案错误";
        }
        else if(randomresult==3){
            result = "运行超时";
        }
        else{
            result = "通过";
        }
        SubmitRecord record = new SubmitRecord();
        record.setStatus(result);
        record.setProblemid(problemid);
        record.setRecordid(UUID.randomUUID().toString());
        record.setUserid(loggedUserid);
        recordService.saveRecord(record);
        if(contestid.equals("NONE")){ //正常提交
            historyService.modifyHistory(loggedUserid,problemid,record);
            return "redirect:/record/list?problemid="+problemid+"&page=0";
        }
        else{ //比赛的提交
            return "notfound";
        }
    }
}
